function out = CampbellSchillerRegression_Regime(Data,shortRate,econAct,thresholdLevel,maturities,h)

% IMPORTANT: we have monthly data but we do not necessarily have all
% monthly maturities in the cross-section dimension, i.e. yields of
% maturities 0.1, 0.2, 0.3 months ets. 
% We can easily correct for this by ensuring that one time step in the
% time series dimension also corresponds to one time step in the
% cross-section dimension.
% Suppose we have monthly yields in the time series dimension, but we only 
% observe yields every 6th months in the cross-section dimension. Then, 
% we let h = 6;

regime_exp     = real(econAct >= thresholdLevel); %a boom
regime_rec     = real(econAct < thresholdLevel);  %a recression
T              = size(Data,1);
n              = length(maturities);
CSBetta        = nan(4,n);
CSBetta_tstat  = nan(4,n);

for i=2:n
   Y  = Data(1+h:T,i-1)-Data(1:T-h,i);
   x  = (Data(1:T-h,i)-shortRate(1:T-h,1))*h/(maturities(1,i)-h); 
   X  = [ones(T-h,1).*regime_exp(1:T-h,1)  ...
         x.*regime_exp(1:T-h,1)...
         ones(T-h,1).*regime_rec(1:T-h,1) ...
         x.*regime_rec(1:T-h,1)];
   %bTest = regress(Y, X(:,3:4))
   res                = nwest(Y,X,2*h);
   CSBetta(:,i)       = res.beta;
   CSBetta_tstat(:,i) = res.tstat;
end
out.CSBetta_exp      = CSBetta(1:2,:);
out.CSBetta_tstat_exp= CSBetta_tstat(1:2,:);
out.CSBetta_rec      = CSBetta(3:4,:);
out.CSBetta_tstat_rec= CSBetta_tstat(3:4,:);
out.maturities       = maturities;
out.h                = h;
